package com.jianan.mis.app.overseastudy;

import static com.jianan.mis.util.ui.table.TableCreator.createSortedTableColumn;
import static com.jianan.mis.util.ui.table.TableCreator.createTableColumn;

import java.math.BigDecimal;
import java.text.Collator;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.cache.CountryCache;
import com.jianan.mis.data.cache.EmployeeCache;
import com.jianan.mis.data.dao.base.PageControl;
import com.jianan.mis.data.domain.Country;
import com.jianan.mis.data.domain.Employee;
import com.jianan.mis.data.domain.OsContract;
import com.jianan.mis.data.domain.OsContractPlan;
import com.jianan.mis.data.domain.OsContractState;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.service.OsContractService;
import com.jianan.mis.sys.IGroupKeys;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.ResourceManager;
import com.jianan.mis.sys.SpringBeanFactory;
import com.jianan.mis.util.StringUtils;
import com.jianan.mis.util.ui.table.AbstractInvertableTableSorter;

public class OsContractManagerView extends ViewPart {

	private Label lblResult;
	private Label lblPage;
	private Combo comboState;
	private Combo comboVisaResult;
	private Text txtStudentName;
	private Text txtContractNo;
	private Combo comboMaterialman;
	private Combo comboCountry;
	private Button btnFinish;
	
	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			OsContract osContract = (OsContract) element;
			String text = contracts.getColumn(columnIndex).getText();
			if (text.equals(CONTRACT_ID)) {
				return osContract.getId().toString();
			} else if (text.equals(CONTRACT_NO)) {
				return osContract.getContractNo();
			} else if (text.equals(STUDENT_NAME)) {
				return osContract.getName();
			} else if (text.equals(COUNTRY_NAME)) {
				return osContract.getCountryIntent().getName();
			} else if (text.equals(MATERIALMAN_NAME)) {
				if (osContract.getMaterialman() != null)
					return osContract.getMaterialman().getName();
				else
					return "";
			} else if (text.equals(COLLECT_RATE)) {
				if(osContract.getCollectRate()!=null)
					return osContract.getCollectRate().setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%";
				else
					return "";
			} else if (text.equals(TRANSLATE_RATE)) {
				if(osContract.getTranslateRate()!=null)
					return osContract.getTranslateRate().setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%";
				else	
					return "";
			} else if (text.equals(SIGNUP_DATE)) {
				return dateFormat.format(osContract.getSignupDate());
			} else if (text.equals(HANDOVER_DATE)) {
				if (osContract.getHandoverDate() != null)
					return dateFormat.format(osContract.getHandoverDate());
				else
					return "";
			} else if (text.equals(UPDATE_DATE)) {
				return dateFormat.format(osContract.getUpdateDate());
			} else if (text.equals(VISA_RESULT)) {
				return osContract.getVisaResult();
			} else if (text.equals(STATE)) {
				Integer state = osContract.getState();
				if(state.equals(OsContractState.SIGNED)) {					
					return "已签约";
				} else if(state.equals(OsContractState.HANDOVERED)) {				
					OsContractPlan osContactPlan = osContractService.getPlan(osContract);
					if (osContactPlan != null) {
						if (osContactPlan.getMaterialState().equals(0))
							return "已移交材料部";
						else
							return "等待语言成绩";
					} else {
						return "";
					}
				} else if(state.equals(OsContractState.MATERIAL_PREPARED)) {				
					return "完成材料准备";
				} else if(state.equals(OsContractState.SENT)) {				
					return "已送签";
				} else if(state.equals(OsContractState.VISA_FINISHED)) {				
					return "得到签证结果";
				} else if(state.equals(OsContractState.FINANCE_FINISHED)) {				
					return "已财务审结";
				} else if(state.equals(OsContractState.FINISHED)) {	
					return "已结案";
				} else if(state.equals(OsContractState.EXPIRED)) {				
					return "已过期";
				} else if(state.equals(OsContractState.FINANCE_REVOKED)) {				
					return "已审结废除";
				} else if(state.equals(OsContractState.REVOKED)) {				
					return "合同废除";
				} else {
					return "";
				}
			} else {
				return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	
	public static final String ID = "com.jianan.mis.app.overseastudy.OsContractManagerView"; //$NON-NLS-1$

	private TableViewer contractTableViewer;
	private Table contracts;
	final private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
	final private int PAGE_SIZE = 15;
	private PageControl pageControl;
	private Map conditions;

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		final FillLayout fillLayout = new FillLayout();
		fillLayout.marginWidth = 10;
		fillLayout.marginHeight = 10;
		parent.setLayout(fillLayout);
		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label.setText("查询条件:");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_1.setText("学生姓名:");

		txtStudentName = new Text(container, SWT.BORDER);
		final GridData gd_txtStudentName = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtStudentName.widthHint = 120;
		txtStudentName.setLayoutData(gd_txtStudentName);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("国家:");

		comboCountry = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboCountry = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboCountry.setLayoutData(gd_comboCountry);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_3.setText("材料员:");

		comboMaterialman = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboMaterialman = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboMaterialman.setLayoutData(gd_comboMaterialman);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_4.setText("合同号:");

		txtContractNo = new Text(container, SWT.BORDER);
		final GridData gd_txtContractNo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtContractNo.widthHint = 148;
		txtContractNo.setLayoutData(gd_txtContractNo);

		final Label label_5 = new Label(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_5.setText("签证结果:");

		comboVisaResult = new Combo(container, SWT.READ_ONLY);
		comboVisaResult.setItems(new String[] {"获签", "拒签"});
		final GridData gd_comboVisaResult = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboVisaResult.setLayoutData(gd_comboVisaResult);

		final Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_6.setText("签约档案状态:");

		comboState = new Combo(container, SWT.READ_ONLY);
		comboState.setItems(new String[] {"已签约", "已移交材料部", "材料准备完成", "已送签", "得到签证结果", "已财务审结", "已结案", "已过期", "已审结废除", "合同废除"});
		final GridData gd_comboState = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboState.setLayoutData(gd_comboState);
		
		new Label(container, SWT.NONE);

		final Button btnQuery = new Button(container, SWT.NONE);
		btnQuery.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				pageControl = new PageControl(PAGE_SIZE);
				buildQueryConditions();
				pageQuery();
			}
		});
		final GridData gd_btnQuery = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnQuery.setLayoutData(gd_btnQuery);
		btnQuery.setText("查询");
		btnQuery.setImage(ImageManager.getImage(IImageKeys.SEARCH));

		final Button btnReset = new Button(container, SWT.NONE);
		btnReset.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				txtStudentName.setText("");
				comboMaterialman.deselectAll();
				comboCountry.deselectAll();
				txtContractNo.setText("");
				comboVisaResult.deselectAll();
				comboState.deselectAll();
			}
		});
		final GridData gd_btnReset = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnReset.setLayoutData(gd_btnReset);
		btnReset.setText("重置");
		btnReset.setImage(ImageManager.getImage(IImageKeys.RESET));

		final Label label_7 = new Label(container, SWT.NONE);
		label_7.setFont(ResourceManager.getFont("", 10, SWT.BOLD));
		label_7.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 4, 1));
		label_7.setText("查询结果:");
		
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		lblResult = new Label(container, SWT.NONE);
		lblResult.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		lblResult.setText("");

		lblPage = new Label(container, SWT.NONE);
		lblPage.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		lblPage.setText("");		
		
		final Button btnFirstPage = new Button(container, SWT.NONE);
		btnFirstPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageCount() > 0) {
					pageControl.setPageNo(1);
					pageQuery();
				}
			}
		});
		final GridData gd_btnFirstPage = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnFirstPage.setLayoutData(gd_btnFirstPage);
		btnFirstPage.setText("首页");
		btnFirstPage.setImage(ImageManager.getImage(IImageKeys.LEFT));

		final Button btnPrevPage = new Button(container, SWT.NONE);
		btnPrevPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageNo() - 1 > 0) {
					pageControl.setPageNo(pageControl.getPageNo() - 1);
					pageQuery();
				}
			}
		});
		final GridData gd_btnPrevPage = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		btnPrevPage.setLayoutData(gd_btnPrevPage);
		btnPrevPage.setText("上页");
		btnPrevPage.setImage(ImageManager.getImage(IImageKeys.UP));

		final Button btnNextPage = new Button(container, SWT.NONE);
		btnNextPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageNo() + 1 <= pageControl.getPageCount()) {
					pageControl.setPageNo(pageControl.getPageNo() + 1);
					pageQuery();
				}
			}
		});
		btnNextPage.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		btnNextPage.setText("下页");
		btnNextPage.setImage(ImageManager.getImage(IImageKeys.DOWN));

		final Button btnLastPage = new Button(container, SWT.NONE);
		btnLastPage.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				if (pageControl.getPageCount() > 0) {
					pageControl.setPageNo(pageControl.getPageCount());
					pageQuery();
				}
			}
		});
		btnLastPage.setLayoutData(new GridData());
		btnLastPage.setText("尾页");
		btnLastPage.setImage(ImageManager.getImage(IImageKeys.RIGHT));

		contractTableViewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
		contractTableViewer.setLabelProvider(new TableLabelProvider());
		contractTableViewer.setContentProvider(new ContentProvider());
		contracts = contractTableViewer.getTable();
		contracts.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) contractTableViewer
						.getSelection();
				OsContract osContract = (OsContract) selection
						.getFirstElement();
				if (osContract == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsContractDialog dialog = new OsContractDialog(
							getViewSite().getShell());
					dialog.setOsContract(osContract);
					// 可修改
					dialog.setReadonly(false);
					if (dialog.open() == Window.OK) {
						pageControl = new PageControl(PAGE_SIZE);
						pageQuery();
					}
				}
			}
		});
		contracts.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) contractTableViewer
						.getSelection();
				OsContract osContract = (OsContract) selection
						.getFirstElement();
				if(osContract.getState().equals(OsContractState.FINANCE_FINISHED)) {
					btnFinish.setText("签约结案");
					btnFinish.setEnabled(true);
				} else if(osContract.getState().equals(OsContractState.FINANCE_REVOKED)) {
					btnFinish.setText("废除合同");
					btnFinish.setEnabled(true);
				} else {
					btnFinish.setEnabled(false);
				}
			}
		});
		contracts.setLinesVisible(true);
		contracts.setHeaderVisible(true);
		final GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
		gd_table.heightHint = 150;
		contracts.setLayoutData(gd_table);

		btnFinish = new Button(container, SWT.NONE);
		btnFinish.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) contractTableViewer
						.getSelection();
				OsContract osContract = (OsContract) selection
						.getFirstElement();
				if (osContract == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					if (osContract.getState().equals(OsContractState.FINANCE_FINISHED)) {
						osContract.setState(OsContractState.FINISHED);
						osContractService.update(osContract);
						MessageDialog.openInformation(getViewSite().getShell(),
								IMessageKeys.SUCCESS, "结案成功!");
					} else if(osContract.getState().equals(OsContractState.FINANCE_REVOKED)) {
						osContract.setState(OsContractState.REVOKED);
						osContractService.update(osContract);
						MessageDialog.openInformation(getViewSite().getShell(),
								IMessageKeys.SUCCESS, "合同废除成功!");
					}
					pageControl = new PageControl(PAGE_SIZE);
					pageQuery();
				}
			}
		});
		btnFinish.setText("签约结案");
		final GridData gd_btnFinish = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnFinish.setLayoutData(gd_btnFinish);
		btnFinish.setImage(ImageManager.getImage(IImageKeys.OK));

		final Button btnView = new Button(container, SWT.NONE);
		btnView.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) contractTableViewer
						.getSelection();
				OsContract osContract = (OsContract) selection
						.getFirstElement();
				if (osContract == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsContractDialog dialog = new OsContractDialog(
							getViewSite().getShell());
					dialog.setOsContract(osContract);
					// 可修改
					dialog.setReadonly(false);
					if (dialog.open() == Window.OK) {
						pageControl = new PageControl(PAGE_SIZE);
						pageQuery();
					}
				}
			}
		});
		btnView.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1));
		btnView.setText("查看");
		btnView.setImage(ImageManager.getImage(IImageKeys.FORM));

		final Button btnSum = new Button(container, SWT.NONE);
		btnSum.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (pageControl == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先进行查询!");
					return;
				}
				IStructuredSelection selection = (IStructuredSelection) contractTableViewer
						.getSelection();
				OsContract osContract = (OsContract) selection
						.getFirstElement();
				if (osContract == null) {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请先选择一条记录!");
				} else {
					OsFinanceDialog dialog = new OsFinanceDialog(getViewSite().getShell());
					dialog.setOsContract(osContract);
					dialog.setMode(1);
					dialog.open();
				}
			}
		});
		final GridData gd_btnSum = new GridData();
		btnSum.setLayoutData(gd_btnSum);
		btnSum.setText("财务明细");
		btnSum.setImage(ImageManager.getImage(IImageKeys.OTHER));
		
		initializeTableViewer();
		initializeServices();
		setupValues();
		filterPermission();
		notice();
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	public static final String CONTRACT_ID = "签约档案号";
	public static final String CONTRACT_NO = "合同号";
	public static final String STUDENT_NAME = "签约学生";
	public static final String COUNTRY_NAME = "签约国家";
	public static final String MATERIALMAN_NAME = "材料员";
	public static final String COLLECT_RATE = "材料收集率";
	public static final String TRANSLATE_RATE = "材料翻译率";
	public static final String SIGNUP_DATE = "签约日期";
	public static final String HANDOVER_DATE = "移交材料部日期";
	public static final String UPDATE_DATE = "最后更新日期";
	public static final String VISA_RESULT = "签证结果";
	public static final String STATE = "状态";
	
	private void initializeTableViewer() {
		/*createSortedTableColumn(contractTableViewer, SWT.CENTER, CONTRACT_ID, "", 120,
				new ContractIdSorter(), SWT.UP, false, null).chooseColumnForSorting();*/	
		createSortedTableColumn(contractTableViewer, SWT.CENTER, CONTRACT_NO, "", 120,
				new ContractNoSorter(), SWT.UP, false, null).chooseColumnForSorting();	
		createSortedTableColumn(contractTableViewer, SWT.CENTER, STUDENT_NAME, "", 120,
				new StudentNameSorter(), SWT.UP, false, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, COUNTRY_NAME, "", 120,
				new CountryNameSorter(), SWT.UP, false, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, MATERIALMAN_NAME, "", 120,
				new MaterialManSorter(), SWT.UP, false, null);
		createTableColumn(contractTableViewer, SWT.CENTER, COLLECT_RATE, "", 100, null);
		createTableColumn(contractTableViewer, SWT.CENTER, TRANSLATE_RATE, "", 100, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, SIGNUP_DATE, "", 170,
				new SignupDateSorter(), SWT.UP, false, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, HANDOVER_DATE, "", 170,
				new HandoverDateSorter(), SWT.UP, false, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, UPDATE_DATE, "", 170,
				new UpdateDateSorter(), SWT.UP, false, null);
		createSortedTableColumn(contractTableViewer, SWT.CENTER, VISA_RESULT, "", 170,
				new VisaResultSorter(), SWT.UP, false, null);
		createTableColumn(contractTableViewer, SWT.CENTER, STATE, "", 100, null);
	}
	
	List<Integer> materialmanIds = new ArrayList<Integer>();
	List<Integer> countryIds = new ArrayList<Integer>();
	
	private void setupValues() {
		List<Employee> consultants = EmployeeCache.getInstance().getEmployeesByGroups(
				new String[] { IGroupKeys.OSMATERIALMAN, IGroupKeys.OSMATERIALCHIEF });
		if (consultants != null) {
			for (Employee employee : consultants) {
				materialmanIds.add(employee.getId());
				comboMaterialman.add(employee.getName());
			}
		}
		List<Country> countries = CountryCache.getInstance().getAllCountries();
		if (countries != null) {
			for (Country country : countries) {
				countryIds.add(country.getId());
				comboCountry.add(country.getName());
			}
		}
	}
	
	private void filterPermission() {
		if (!Session.getInstance().getGroups().contains(IGroupKeys.FIN_DEP)
				&& !Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
		}
	}
	
	private void notice() {
		// 结案通知
		if (Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
			List<OsContract> contracts = osContractService
					.getByStates(new int[] { OsContractState.FINANCE_FINISHED, OsContractState.FINANCE_REVOKED });
			for (OsContract contract : contracts) {
				StringBuffer noticeMsg = new StringBuffer();
				noticeMsg.append("合同号为 ");
				noticeMsg.append(contract.getContractNo());
				noticeMsg.append(" ,学生名称为 ");
				noticeMsg.append(contract.getName());
				noticeMsg.append(" 的签约档案");
				if (contract.getState().equals(OsContractState.FINANCE_FINISHED)) {
					noticeMsg.append("已财务审结!");
				} else if (contract.getState().equals(OsContractState.FINANCE_REVOKED)) {
					noticeMsg.append("已审结废除!");
				}
				MessageDialog.openInformation(getViewSite().getShell(),
						IMessageKeys.INFORMATION, noticeMsg.toString());
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	private void buildQueryConditions() {
		conditions = new HashMap();
		if (StringUtils.isNotEmpty(txtStudentName.getText())) {
			conditions.put("studentName", txtStudentName.getText().trim());
		}
		if (comboCountry.getSelectionIndex() >= 0) {
			conditions.put("countryId", countryIds.get(comboCountry.getSelectionIndex()));
		}
		if (comboMaterialman.getSelectionIndex() >= 0) {
			conditions.put("materialmanId", materialmanIds.get(comboMaterialman.getSelectionIndex()));
		}
		if (StringUtils.isNotEmpty(txtContractNo.getText())) {
			conditions.put("contractNo", txtContractNo.getText().trim());
		}
		if (comboState.getSelectionIndex() >= 0) {
			conditions.put("state", comboState.getSelectionIndex());
		}
		if (comboVisaResult.getSelectionIndex() == 0) {
			conditions.put("visaResult", "获签");
		} else if (comboVisaResult.getSelectionIndex() == 1) {
			conditions.put("visaResult", "拒签");
		}
	}
	
	private void pageQuery() {
		osContractService.pageQuery(pageControl, conditions);
		contractTableViewer.setInput(pageControl.getPageData());
		lblResult.setText("查询结果共 " + pageControl.getRowCount() + " 条记录");
		lblPage.setText("页码(每页显示" + PAGE_SIZE + "条): " + pageControl.getPageNo() + "/" + pageControl.getPageCount());
		contracts.deselectAll();
	}

	private OsContractService osContractService;
	
	private void initializeServices() {
		osContractService = (OsContractService) SpringBeanFactory.getBean("osContractService");
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	// Convenience class which avoids casting in subclasses
	private abstract class AbstractSorter extends AbstractInvertableTableSorter {
		public int compare(Viewer viewer, Object o1, Object o2) {
			return compare((OsContract) o1, (OsContract) o2);
		}
		public abstract int compare(OsContract c1, OsContract c2);
	}
	/*private class ContractIdSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return NumberUtils.compare(c1.getId(), c2.getId());
		}
	}*/
	private class ContractNoSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return Collator.getInstance().compare(c1.getContractNo(),
					c2.getContractNo());
		}
	}
	private class StudentNameSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return Collator.getInstance().compare(c1.getName(), c2.getName());
		}
	}
	private class CountryNameSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return Collator.getInstance().compare(
					c1.getCountryIntent().getName(),
					c2.getCountryIntent().getName());
		}
	}
	private class MaterialManSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return Collator.getInstance().compare(c1.getMaterialman().getName(),
					c2.getMaterialman().getName());
		}
	}
	private class SignupDateSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return c1.getSignupDate().compareTo(c2.getSignupDate());
		}
	}
	private class HandoverDateSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return c1.getHandoverDate().compareTo(c2.getHandoverDate());
		}
	}
	private class UpdateDateSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return c1.getUpdateDate().compareTo(c2.getUpdateDate());
		}
	}
	private class VisaResultSorter extends AbstractSorter {
		public int compare(OsContract c1, OsContract c2) {
			return Collator.getInstance().compare(c1.getVisaResult(),
					c2.getVisaResult());
		}
	}
}
